package com.inspur.sc.springboot.utils;

import com.inspur.sc.springboot.config.HadoopProperties;
import com.inspur.sc.springboot.vo.UploadFileVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class HadoopUtil {

    @Autowired
    private  HadoopProperties properties;

    /**
     * 文件上传
     * @param inputStream
     * @param dst
     * @param overwrite
     * @return
     */
    public boolean hdfsUpload(InputStream inputStream,String dst,boolean overwrite){
        try {
            String uri=properties.getConfig().get("fs.defaultFS");
            FileSystem fileSystem=FileSystem.get(new URI(uri),properties.getConfig(),"root");
            FSDataOutputStream outputStream=fileSystem.create(new Path(dst),overwrite);
            IOUtils.copyBytes(inputStream,outputStream,10240,true);
            return true;
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取文件或者目录信息，包括目录中的文件信息
     * @param filePath
     * @return
     * @throws IOException
     */
    public List<UploadFileVO> getFiles(String filePath) {
        String uri=properties.getConfig().get("fs.defaultFS");
        try {
            FileSystem fileSystem= FileSystem.get(new URI(uri),properties.getConfig(),"root");
            FileStatus[] fileStatuses=fileSystem.listStatus(new Path(filePath));
            List<UploadFileVO> fileVOList=new ArrayList<>();
            for (FileStatus fileStatus : fileStatuses) {
                UploadFileVO fileVO=new UploadFileVO(
                        fileStatus.getPath().getName(),
                        StringUtil.getFileExtension(fileStatus.getPath().getName()),
                        Long.toString(fileStatus.getLen()),
                        fileStatus.getPath().toString());
                fileVOList.add(fileVO);
            }
            fileSystem.close();
            return fileVOList;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取hdfs的流数据
     * @param file
     * @throws IOException
     */
    public InputStream getFile(String file){
        String uri=properties.getConfig().get("fs.defaultFS");
        try {
            FileSystem fileSystem = FileSystem.get(new URI(uri),properties.getConfig(),"root");
            boolean exists=fileSystem.exists(new Path(file));
            if(exists){
                FSDataInputStream inputStream=fileSystem.open(new Path(file));
                return inputStream;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 删除文件/目录
     * @param filePath 文件路径/目录路径
     * @param recursive true 递归删除，false 普通删除
     * @return
     */
    public boolean delete(String filePath,boolean recursive){
        try {
            String uri=properties.getConfig().get("fs.defaultFS");
            FileSystem fileSystem=FileSystem.get(new URI(uri),properties.getConfig(),"root");
            fileSystem.delete(new Path(filePath),recursive);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return false;
    }

    /**
     * 创建目录
     * @param dir
     * @return
     */
    public boolean mkdir(String dir){
        try{
            String uri=properties.getConfig().get("fs.defaultFS");
            FileSystem fileSystem=FileSystem.get(new URI(uri),properties.getConfig(),"root");
            if(!fileSystem.exists(new Path(dir))){
                fileSystem.mkdirs(new Path(dir));
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return false;
    }
}
